using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using RealState.BusinessObjects;

namespace RealState.DAL
{
	public class UsersAddressDAO
	{
		public UsersAddressDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<UsersAddress> GetUsersAddresss()
		{
			try
			{
				List<UsersAddress> lstUsersAddresss = new List<UsersAddress>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTUsersAddresss",CommandType.StoredProcedure);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					UsersAddress oUsersAddress = new UsersAddress();
					oUsersAddress.UserId = Convert.ToInt32(oDbDataReader["UserId"]);
					oUsersAddress.Street = Convert.ToString(oDbDataReader["Street"]);

					if(oDbDataReader["DistrictId"] != DBNull.Value)
						oUsersAddress.DistrictId = Convert.ToInt32(oDbDataReader["DistrictId"]);

					if(oDbDataReader["CityId"] != DBNull.Value)
						oUsersAddress.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
					lstUsersAddresss.Add(oUsersAddress);
				}
				oDbDataReader.Close();
				return lstUsersAddresss;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public UsersAddress GetUsersAddress(int UserId)
		{
			try
			{
				UsersAddress oUsersAddress = new UsersAddress();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTUsersAddress",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserId",DbType.Int32,UserId));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oUsersAddress.UserId = Convert.ToInt32(oDbDataReader["UserId"]);
					oUsersAddress.Street = Convert.ToString(oDbDataReader["Street"]);

					if(oDbDataReader["DistrictId"] != DBNull.Value)
						oUsersAddress.DistrictId = Convert.ToInt32(oDbDataReader["DistrictId"]);

					if(oDbDataReader["CityId"] != DBNull.Value)
						oUsersAddress.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
				}
				oDbDataReader.Close();
				return oUsersAddress;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddUsersAddress(UsersAddress oUsersAddress)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERTUsersAddress",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Street",DbType.String,oUsersAddress.Street));
				if (oUsersAddress.DistrictId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId",DbType.Int32,oUsersAddress.DistrictId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId",DbType.Int32,DBNull.Value));
				if (oUsersAddress.CityId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId",DbType.Int32,oUsersAddress.CityId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId",DbType.Int32,DBNull.Value));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserID", DbType.Int32, oUsersAddress.UserId));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateUsersAddress(UsersAddress oUsersAddress)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATEUsersAddress",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Street",DbType.String,oUsersAddress.Street));
				if (oUsersAddress.DistrictId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId",DbType.Int32,oUsersAddress.DistrictId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictId",DbType.Int32,DBNull.Value));
				if (oUsersAddress.CityId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId",DbType.Int32,oUsersAddress.CityId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId",DbType.Int32,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserId",DbType.Int32,oUsersAddress.UserId));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveUsersAddress(UsersAddress oUsersAddress)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEUsersAddress",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserId",DbType.Int32,oUsersAddress.UserId));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveUsersAddress(int UserId)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEUsersAddress",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserId",DbType.Int32,UserId));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
